package cn.com.cetccst.datacenter.policy;

import android.content.Context;

import androidx.lifecycle.LiveData;
import androidx.room.Room;

import java.util.List;

import cn.com.cetccst.datacenter.policy.dao.BWAppDao;
import cn.com.cetccst.datacenter.policy.dao.ComplianceDao;
import cn.com.cetccst.datacenter.policy.dao.ComplianceRecordDao;
import cn.com.cetccst.datacenter.policy.dao.PolicyDao;
import cn.com.cetccst.datacenter.policy.dao.RestrictionDao;
import cn.com.cetccst.datacenter.policy.dao.RestrictionStatusDao;
import cn.com.cetccst.datacenter.policy.entity.BWAppEntity;
import cn.com.cetccst.datacenter.policy.entity.ComplianceEntity;
import cn.com.cetccst.datacenter.policy.entity.ComplianceRecordEntity;
import cn.com.cetccst.datacenter.policy.entity.PolicyEntity;
import cn.com.cetccst.datacenter.policy.entity.RestrictionEntity;
import cn.com.cetccst.datacenter.policy.entity.RestrictionStatusEntity;

public class PolicyRepository {
    private final PolicyDatabase policyDatabase;
    private static PolicyRepository instance;
    private final PolicyDao policyDao;
    private final ComplianceDao complianceDao;
    private final ComplianceRecordDao complianceRecordDao;
    private final RestrictionDao restrictionDao;
    private final RestrictionStatusDao restrictionStatusDao;
    private final BWAppDao bwAppDao;

    private PolicyRepository(Context context) {
        policyDatabase = Room.databaseBuilder(context, PolicyDatabase.class, "policy.db")
                .allowMainThreadQueries()
                .build();
        policyDao = policyDatabase.getPolicyDao();
        complianceDao = policyDatabase.getComplianceDao();
        complianceRecordDao = policyDatabase.getCompliancePendingDao();
        restrictionDao = policyDatabase.getRestrictionDao();
        restrictionStatusDao = policyDatabase.getRestrictionStatusDao();
        bwAppDao = policyDatabase.getBWAppDao();
    }

    /**
     * 在存储的模块中初始化
     *
     * @param context
     */
    public static void initialize(Context context) {
        if (instance == null)
            instance = new PolicyRepository(context);
    }

    public static PolicyRepository getInstance() {
        return instance;
    }

    public void savePolicy(PolicyEntity policy) {
        policyDao.insert(policy);
    }

    public LiveData<PolicyEntity> getPolicy(String policy) {
        return policyDao.query(policy);
    }

    public LiveData<List<PolicyEntity>> getAllPolicies() {
        return policyDao.queryAll();
    }

    public void saveCompliance(ComplianceEntity entity) {
        complianceDao.update(entity);
    }

    public void saveAllCompliance(List<ComplianceEntity> entities) {
        policyDatabase.runInTransaction(() -> {
            complianceDao.clearAll();
            complianceDao.insertAll(entities);
        });
    }

    public LiveData<List<ComplianceEntity>> getAllCompliance() {
        return complianceDao.loadAll();
    }

    public void saveComplianceRecords(List<ComplianceRecordEntity> entities) {
        complianceRecordDao.insertAll(entities);
    }

    public LiveData<Long> getLatestDateInComplianceRecord() {
        return complianceRecordDao.getLatestDate();
    }

    public List<ComplianceRecordEntity> getComplianceRecordsByDate(long date) {
        return complianceRecordDao.getRecordsByDate(date);
    }

    public void deleteComplianceRecordsByDate(long date) {
        complianceRecordDao.deleteRecordsByDate(date);
    }

    public void saveAllRestriction(List<RestrictionEntity> entities) {
        policyDatabase.runInTransaction(() -> {
            restrictionDao.clearAll();
            restrictionDao.insertAll(entities);
        });
    }

    //下发得MDM策略信息==>
    public LiveData<List<RestrictionEntity>> getAllRestriction() {
        return restrictionDao.queryAll();
    }

    public LiveData<List<RestrictionEntity>> loadAllDisabled() {
        return restrictionDao.queryAllDisabled();
    }

    public LiveData<Integer> getStatus(String restrictionName) {
        return restrictionDao.getStatus(restrictionName);
    }
    //<==下发得MDM策略信息


    //本地得MDM策略执行情况==>
    public LiveData<List<RestrictionStatusEntity>> loadRestrictionStatus() {
        return restrictionStatusDao.loadRestrictions();
    }

    public List<RestrictionStatusEntity> getRestrictionStatus() {
        return restrictionStatusDao.getRestrictions();
    }

    public void saveAllRestrictionStatus(List<RestrictionStatusEntity> entities) {
        restrictionStatusDao.insertAll(entities);
    }
    //<==本地得MDM策略执行情况

    public void saveAllBWApps(List<BWAppEntity> entities) {
        policyDatabase.runInTransaction(() -> {
            bwAppDao.clearAll();
            bwAppDao.insertAll(entities);
        });
    }

    public LiveData<List<BWAppEntity>> getAllBWAppEntity() {
        return bwAppDao.loadAll();
    }

    public List<BWAppEntity> getAllBlackAppEntity() {
        return bwAppDao.getBlackApps();
    }

}
